/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aprendizajePorRefuerzo.interfaz;

import aprendizajePorRefuerzo.agente.Agente;
import aprendizajePorRefuerzo.agente.AgenteImp;
import aprendizajePorRefuerzo.agente.AgenteSarsaLambda;
import aprendizajePorRefuerzo.controlador.Controlador;
import aprendizajePorRefuerzo.entorno.EntornoDinamicoConViento;
import aprendizajePorRefuerzo.entorno.EntornoGrilla;
import aprendizajePorRefuerzo.politica.Politica;
import java.awt.BorderLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;

/**
 *
 * @author Fernando Sorzana
 */
public class FrmPrincipal extends javax.swing.JFrame {
    
    private Controlador controlador;
    
    private FabricaPanelesAgentes fabricaAgentes;
    
    private FabricaPanelesPoliticas fabricaPoliticas;
    
    private PnlGrilla panelGrilla;
    
    private PnlColumnas panelColumnas;
    
    private PnlParametrosAgente panelAgente;
    
    private PnlParametrosPolitica panelPolitica;
    
    private int numeroEtapas;

    /**
     * Creates new form FrmPrincipal
     */
    public FrmPrincipal() {
        initComponents();
        controlador = new Controlador();
        controlador.setEntorno(new EntornoDinamicoConViento(0,0));
        fabricaAgentes = new FabricaPanelesAgentes();
        fabricaPoliticas = new FabricaPanelesPoliticas();
        cmbAgentes.setModel(new DefaultComboBoxModel(controlador.listarAgentesPosibles().toArray()));
        cmbAgentes.setSelectedIndex(0);
        cmbPoliticas.setModel(new DefaultComboBoxModel(controlador.listarPoliticasPosibles().toArray()));
        cmbPoliticas.setSelectedIndex(0);
        seleccionarAgente();
        seleccionarPolitica();
        txtNumeroEpisodios.setValue(1000);
        seleccionarNumeroEpisodios(1000);
        txtMaximoNumeroPasos.setValue(1000);
        seleccionarMaximoNumeroPasos(1000);
        txtEpisodiosEntreInformes.setValue(100);
        seleccionarEpisodiosEntreInformes(100);
        panelGrilla = new PnlGrilla();
        numeroEtapas = 0;
        panelColumnas = new PnlColumnas();
        panelColumnas.setNumeroColumnas(3);
        pnlContenedorViento.add(panelColumnas);
        txtNumeroFilas.setValue(3);
        seleccionarNumeroFilas(3);
        txtNumeroColumnas.setValue(3);
        seleccionarNumeroColumnas(3);
        pnlTab.addTab("Etapa " + numeroEtapas, panelGrilla);
        numeroEtapas ++;
        panelGrilla.setEtapa(((EntornoDinamicoConViento)controlador.getEntorno()).getUltimaEtapa());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        pnlContenedorParametrosAgente = new javax.swing.JPanel();
        cmbAgentes = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        cmbPoliticas = new javax.swing.JComboBox();
        pnlContenedorParametrosPolitica = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNumeroEpisodios = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMaximoNumeroPasos = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        txtEpisodiosEntreInformes = new javax.swing.JFormattedTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtNumeroFilas = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        txtNumeroColumnas = new javax.swing.JFormattedTextField();
        btnCrearEntorno = new javax.swing.JButton();
        pnlGrillaContenedor = new javax.swing.JPanel();
        pnlTab = new javax.swing.JTabbedPane();
        pnlContenedorViento = new javax.swing.JPanel();
        btnEjecutar = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        rdbNormal = new javax.swing.JRadioButton();
        rdbRey = new javax.swing.JRadioButton();
        chkQuieto = new javax.swing.JCheckBox();
        btnNuevaEtapa = new javax.swing.JButton();
        txtEpisodioInicio = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        chkRandom = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Agente"));

        pnlContenedorParametrosAgente.setLayout(new java.awt.BorderLayout());

        cmbAgentes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbAgentes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbAgentesItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlContenedorParametrosAgente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmbAgentes, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbAgentes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlContenedorParametrosAgente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Política"));

        cmbPoliticas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbPoliticas.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbPoliticasItemStateChanged(evt);
            }
        });

        pnlContenedorParametrosPolitica.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlContenedorParametrosPolitica, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmbPoliticas, 0, 302, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbPoliticas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlContenedorParametrosPolitica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Episodios"));

        jLabel3.setText("Número de episodios:");

        txtNumeroEpisodios.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        txtNumeroEpisodios.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNumeroEpisodiosFocusLost(evt);
            }
        });

        jLabel4.setText("Máximo número de pasos:");

        txtMaximoNumeroPasos.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        txtMaximoNumeroPasos.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMaximoNumeroPasosFocusLost(evt);
            }
        });

        jLabel1.setText("Cantidad de episodios entre informes:");

        txtEpisodiosEntreInformes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(61, 61, 61)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaximoNumeroPasos)
                            .addComponent(txtNumeroEpisodios)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEpisodiosEntreInformes)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNumeroEpisodios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtMaximoNumeroPasos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtEpisodiosEntreInformes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Entorno"));

        jLabel5.setText("Número de filas:");

        txtNumeroFilas.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        txtNumeroFilas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNumeroFilasFocusLost(evt);
            }
        });

        jLabel6.setText("Número de columnas:");

        txtNumeroColumnas.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        txtNumeroColumnas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtNumeroColumnasFocusLost(evt);
            }
        });

        btnCrearEntorno.setText("Crear entorno");
        btnCrearEntorno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrearEntornoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNumeroFilas, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                            .addComponent(txtNumeroColumnas)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCrearEntorno)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNumeroFilas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNumeroColumnas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCrearEntorno))
        );

        pnlGrillaContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder("Propiedades Entorno"));
        pnlGrillaContenedor.setLayout(new java.awt.BorderLayout());
        pnlGrillaContenedor.add(pnlTab, java.awt.BorderLayout.CENTER);

        pnlContenedorViento.setPreferredSize(new java.awt.Dimension(100, 25));
        pnlContenedorViento.setLayout(new java.awt.BorderLayout());
        pnlGrillaContenedor.add(pnlContenedorViento, java.awt.BorderLayout.PAGE_END);

        btnEjecutar.setText("Ejecutar");
        btnEjecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEjecutarActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Movimientos"));

        buttonGroup1.add(rdbNormal);
        rdbNormal.setText("Normal");

        buttonGroup1.add(rdbRey);
        rdbRey.setText("Rey");

        chkQuieto.setText("Permitir quedarse quieto");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(rdbNormal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rdbRey))
                    .addComponent(chkQuieto))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdbNormal)
                    .addComponent(rdbRey))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chkQuieto))
        );

        btnNuevaEtapa.setText("Nueva Etapa");
        btnNuevaEtapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaEtapaActionPerformed(evt);
            }
        });

        jLabel2.setText("Episodio inicio:");

        chkRandom.setText("Viento aleatorio");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(pnlGrillaContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkRandom)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnNuevaEtapa))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(78, 78, 78)
                                        .addComponent(btnEjecutar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtEpisodioInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlGrillaContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEjecutar)
                    .addComponent(btnNuevaEtapa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEpisodioInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chkRandom)
                .addContainerGap(102, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbAgentesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbAgentesItemStateChanged
        seleccionarAgente();
    }//GEN-LAST:event_cmbAgentesItemStateChanged

    private void cmbPoliticasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbPoliticasItemStateChanged
        seleccionarPolitica();
    }//GEN-LAST:event_cmbPoliticasItemStateChanged

    private void txtNumeroEpisodiosFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNumeroEpisodiosFocusLost
        if (txtNumeroEpisodios.getValue() != null){
            seleccionarNumeroEpisodios((int)txtNumeroEpisodios.getValue());
        }
    }//GEN-LAST:event_txtNumeroEpisodiosFocusLost

    private void txtMaximoNumeroPasosFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMaximoNumeroPasosFocusLost
        if (txtMaximoNumeroPasos.getValue() != null){
            seleccionarMaximoNumeroPasos((int)txtMaximoNumeroPasos.getValue());
        }
    }//GEN-LAST:event_txtMaximoNumeroPasosFocusLost

    private void txtNumeroFilasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNumeroFilasFocusLost

    }//GEN-LAST:event_txtNumeroFilasFocusLost

    private void txtNumeroColumnasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNumeroColumnasFocusLost

    }//GEN-LAST:event_txtNumeroColumnasFocusLost

    private void btnCrearEntornoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrearEntornoActionPerformed
        seleccionarNumeroFilas(((Long)txtNumeroFilas.getValue()).intValue());
        seleccionarNumeroColumnas(((Long)txtNumeroColumnas.getValue()).intValue());
    }//GEN-LAST:event_btnCrearEntornoActionPerformed

    private void btnEjecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEjecutarActionPerformed
        panelAgente.actualizarParametros();
        if (AgenteSarsaLambda.class.isAssignableFrom(panelAgente.getAgente().getClass())){
            ((AgenteSarsaLambda)panelAgente.getAgente()).setEstadosPosibles(controlador.getEntorno().getEstados());
        }
        panelPolitica.actualizarParametros();
        controlador.setMovimientos(rdbRey.isSelected(),chkQuieto.isSelected());
        controlador.setNumeroEpisodios(((Integer)txtNumeroEpisodios.getValue()).intValue());
        controlador.setMaximoNumeroPasos(((Integer)txtMaximoNumeroPasos.getValue()).intValue());
        controlador.setEpisodiosEntreInformes(((Integer)txtEpisodiosEntreInformes.getValue()).intValue());
        ((EntornoDinamicoConViento)controlador.getEntorno()).setViento(panelColumnas.getVientos());
        ((EntornoDinamicoConViento)controlador.getEntorno()).setVientoAleatorio(chkRandom.isSelected());
        FrmResultados res = new FrmResultados();
        res.setVisible(true);
        controlador.jugar(res);
    }//GEN-LAST:event_btnEjecutarActionPerformed

    private void btnNuevaEtapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaEtapaActionPerformed
        PnlGrilla panel = new PnlGrilla();
        EntornoDinamicoConViento entorno = (EntornoDinamicoConViento)controlador.getEntorno();
        entorno.nuevaEtapa(Integer.valueOf(txtEpisodioInicio.getText()));
        panel.setEtapa(entorno.getUltimaEtapa());
        panel.setNumeroColumnas(panelGrilla.getNumeroColumnas());
        panel.setNumeroFilas(panelGrilla.getNumeroFilas());
        pnlTab.addTab("Etapa " + numeroEtapas,panel);
        numeroEtapas++;
        panelGrilla = panel;
    }//GEN-LAST:event_btnNuevaEtapaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmPrincipal().setVisible(true);
            }
        });
    }
    
    private void seleccionarAgente(){
        panelAgente = fabricaAgentes.crearPanel(cmbAgentes.getSelectedItem());
        pnlContenedorParametrosAgente.removeAll();
        pnlContenedorParametrosAgente.add(panelAgente);
        controlador.setAgente((AgenteImp)cmbAgentes.getSelectedItem());
        panelAgente.setAgente(controlador.getAgente());
        controlador.getAgente().setPolitica((Politica)cmbPoliticas.getSelectedItem());
        this.validate();
    }
    
    private void seleccionarPolitica(){
        panelPolitica = fabricaPoliticas.crearPanel(cmbPoliticas.getSelectedItem());
        pnlContenedorParametrosPolitica.add(panelPolitica);
        controlador.getAgente().setPolitica((Politica)cmbPoliticas.getSelectedItem());
        panelPolitica.setPolitica(controlador.getAgente().getPolitica());
        this.validate();
    }
    
    private void seleccionarNumeroEpisodios(int numero){
        controlador.setNumeroEpisodios(numero);
    }
    
    private void seleccionarMaximoNumeroPasos(int numero){
        controlador.setMaximoNumeroPasos(numero);
    }
    
    private void seleccionarEpisodiosEntreInformes(int numero){
        controlador.setEpisodiosEntreInformes(numero);
    }
    
    private void seleccionarNumeroFilas(int numero){
        ((EntornoGrilla)controlador.getEntorno()).setCantidadFilas(numero);
        panelGrilla.setNumeroFilas(numero);
    }
    
    private void seleccionarNumeroColumnas(int numero){
        ((EntornoGrilla)controlador.getEntorno()).setCantidadColumnas(numero);
        panelGrilla.setNumeroColumnas(numero);
        panelColumnas.setNumeroColumnas(numero);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCrearEntorno;
    private javax.swing.JButton btnEjecutar;
    private javax.swing.JButton btnNuevaEtapa;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox chkQuieto;
    private javax.swing.JCheckBox chkRandom;
    private javax.swing.JComboBox cmbAgentes;
    private javax.swing.JComboBox cmbPoliticas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel pnlContenedorParametrosAgente;
    private javax.swing.JPanel pnlContenedorParametrosPolitica;
    private javax.swing.JPanel pnlContenedorViento;
    private javax.swing.JPanel pnlGrillaContenedor;
    private javax.swing.JTabbedPane pnlTab;
    private javax.swing.JRadioButton rdbNormal;
    private javax.swing.JRadioButton rdbRey;
    private javax.swing.JTextField txtEpisodioInicio;
    private javax.swing.JFormattedTextField txtEpisodiosEntreInformes;
    private javax.swing.JFormattedTextField txtMaximoNumeroPasos;
    private javax.swing.JFormattedTextField txtNumeroColumnas;
    private javax.swing.JFormattedTextField txtNumeroEpisodios;
    private javax.swing.JFormattedTextField txtNumeroFilas;
    // End of variables declaration//GEN-END:variables
}
